<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['is_admin']=$this->is_admin;
if($_RQ['op']=='list'){	
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>0,
		'status'=>1,
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pcate']){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if($_RQ['ccate']){
		$params['ccate']=intval($_RQ['ccate']);
		$return['params']['ccate']=intval($_RQ['ccate']);
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','star','price','active_plugin','active_id','salenum','total','pluginname','originalprice');
	$orderby='star desc, paixu desc, salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
			
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}else{
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		$storeids=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),'id','id');
		if($storeids){
			$storeids=array_keys($storeids);
		}else{
			$storeids=-1;
		}
		foreach($list as &$itemv){
			$itemv['oldtotal']=$itemv['total'];
			$itemv['alltotal']=pdo_getsum('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],'storeid'=>$storeids),'total');
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]:</br>1、商品总库存情况！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'商品原价','type'=>'string'),
			array('name'=>'alltotal','title'=>'总库存','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'settotal','query'=>'type=out','title'=>'出库'),
			array('type'=>'modal','op'=>'settotal','query'=>'type=in','title'=>'入库'),
			array('type'=>'modal','op'=>'settotal','query'=>'type=diao','title'=>'调拨'),
			array('type'=>'modal','op'=>'settotal','query'=>'type=inmore','title'=>'多店入库'),
		),'batch'=>array(
			//array('type'=>'modal','url'=>'//settotal/','query'=>'type=out','title'=>'一键出库'),
			//array('type'=>'modal','url'=>'//settotal/','query'=>'type=in','title'=>'一键入库'),
			//array('type'=>'modal','url'=>'//settotal/','query'=>'type=diao','title'=>'一键调拨'),
			//array('type'=>'modal','url'=>'//settotal/','query'=>'type=inmore','title'=>'一键多店入库'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','width'=>'20'),
		)
	);
	$return['htmls']=$htmls;
	set_html('title',$$basic['title']?$basic['title']:'商品列表');
	exi($return);
}
if($_RQ['op']=='settotal'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$goodsid=intval($postdata['id']);
		$goodsids=trim($postdata['goodsids']);
		if($goodsids){
			$goodsids=explode(';',$goodsids);
		}else{
			$goodsids=array();
		}
		if($goodsid){
			$goodsids[]=$goodsid;
		}
		if(count($goodsids)==0){
			exi('请先选择商品','error');
		}
		$store_id=intval($postdata['store_id']);
		$tostoreid=intval($postdata['tostoreid']);
		if(!$postdata['num'] && $postdata['type']!='inmore'){
			exi('请填入数量！','error');
		}
		if($postdata['type']=='diao' || $postdata['type']=='in'){
			if(!$tostoreid){
				exi('请选择调入门店！','error');
			}
		}
		if($postdata['type']=='diao' || $postdata['type']=='out'){
			if(!$store_id){
				exi('请选择出库门店！','error');
			}
		}
		foreach($goodsids as $v){
			if($postdata['type']=='diao' || $postdata['type']=='in'){
				$store_goods=pdo_get('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$v,'storeid'=>$tostoreid),array('id','total'));
				$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v),array('id','total','title'));
				if(!$store_goods['id']){
					$data=array(
								'uniacid'=>$_SESSION['uniacid'],
								'goodsid'=>$v,
								'storeid'=>$tostoreid,
								'total'=>intval($goods['total']),
								'price'=>$goods['price'],
					);
					pdo_insert('jy_weishop_store_goods',$data);
				}
			}
			if($postdata['type']=='out' || $postdata['type']=='diao'){
				$lsastnum=pdo_getcolumn('jy_weishop_store_goods',array('goodsid'=>$v,'storeid'=>$store_id),'total');
				if($lsastnum<$postdata['num']){
					exi($goods['title'].'库存数量不足！当前仅剩库存'.$lsastnum,'error');
				}
			}
			if($postdata['type']=='diao'){
				pdo_update('jy_weishop_store_goods',array('total +='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$tostoreid,'uniacid'=>$_SESSION['uniacid']));
			}
			if($postdata['type']=='out' || $postdata['type']=='diao'){
				pdo_update('jy_weishop_store_goods',array('total -='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$store_id,'uniacid'=>$_SESSION['uniacid']));
			}elseif($postdata['type']=='in'){
				pdo_update('jy_weishop_store_goods',array('total +='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$tostoreid,'uniacid'=>$_SESSION['uniacid']));
			}
			if($postdata['type']=='out'){
				$outtype=1;
			}elseif($postdata['type']=='in'){
				$intype=1;
			}elseif($postdata['type']=='diao'){
				$outtype=2;
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'goods_id'=>$v,
					'store_id'=>$tostoreid,
					'num'=>intval($postdata['num']),
					'type'=>1,
					'intype'=>2,
					'outtype'=>0,
					'remark'=>$postdata['remark'],
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_shopwms_log',$data);
			}elseif($postdata['type']=='inmore'){
				$stores=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']));
				foreach($stores as $sv){
					$tonum=$postdata['morenum'.$sv['id']];
					if($tonum>0){
						$data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$_SESSION['uid'],
							'goods_id'=>$v,
							'store_id'=>$sv['id'],
							'num'=>intval($tonum),
							'type'=>1,
							'intype'=>1,
							'outtype'=>0,
							'remark'=>$postdata['remark'],
							'createtime'=>TIMESTAMP,
						);
						pdo_insert('jy_shopwms_log',$data);
						pdo_update('jy_weishop_store_goods',array('total +='=>$tonum),array('goodsid'=>$v,'storeid'=>$sv['id'],'uniacid'=>$_SESSION['uniacid']));
					}
				}
			}
			if($postdata['type']!='inmore'){
				$newstoreid=$postdata['type']=='out' || $postdata['type']=='diao'?$store_id:$tostoreid;
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'goods_id'=>$v,
					'store_id'=>$newstoreid,
					'num'=>intval($postdata['num']),
					'type'=>$postdata['type']!='in'?0:1,
					'intype'=>$intype,
					'outtype'=>$outtype,
					'remark'=>$postdata['remark'],
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_shopwms_log',$data);
			}
		}
		exi('操作成功！','','//list/');
	}
	$item=pdo_get('jy_weishop_goods',array('id'=>intval($_RQ['id'])),array('id','title','total'));
	if(!$item){
		exi('指定商品不存在！','error');
	}
	$type=$_RQ['type'];
	if($type=='out'){
		$desc='出库操作';
	}elseif($type=='in'){
		$desc='入库操作';
	}elseif($type=='diao'){
		$desc='调拨操作';
	}elseif($type=='inmore'){
		$desc='多店入库操作';
	}
	$goodsids=trim($_RQ['ids'],';');
	$item['type']=$type;
	$item['goodsids']=$goodsids;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>$desc,
		'edit'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'copy'),
			array('name'=>'store_id','title'=>'出库门店','type'=>'select','options'=>'stores','show'=>array('type'=>'out')),
			array('name'=>'tostoreid','title'=>'入库门店','type'=>'select','options'=>'stores','show'=>array('type'=>'in')),
			array('name'=>'store_id','title'=>'出库门店','type'=>'select','options'=>'stores','show'=>array('type'=>'diao')),
			array('name'=>'tostoreid','title'=>'入库门店','type'=>'select','options'=>'stores','show'=>array('type'=>'diao')),
		),
	);
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']));
	if($type=='inmore'){
		foreach($return['stores'] as $sv){
			$htmls['edit'][]=array('name'=>'morenum'.$sv['id'],'title'=>$sv['title'].'入库数量','type'=>'string');
		}
	}else{
		$htmls['edit'][]=array('name'=>'num','title'=>'操作数量','type'=>'string');
	}
	$return['htmls']=$htmls;
	exi($return);
}
?>